﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WP7Client.Helpers;
using WP7Client.Classes;

namespace WP7Client.UserInterface.Pages
{
    public partial class SearchResultPage : MSHPhoneApplicationPage
    {
        public Boolean PivotItemsBuilt = false;

        public SearchResultPage()
        {
            InitializeComponent();
            PivotItemsBuilt = false;
            ControllerHelper.SearchResultController.SearchResultPage = this;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            PivotItemsBuilt = false;
            ControllerHelper.SearchResultController.SearchResultPage = this;
        }

        private void MSHPhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            PivotItemsBuilt = false;
            ControllerHelper.SearchResultController.SearchResultPage = this;
            PivotSearchResults.DataContext = ControllerHelper.SearchResultController;
        }

        public void FillPivot(int itemCount, int maxPivotPageItemCount = 10)
        {
            PivotSearchResults.Items.Clear();

            int previousCount = 0;
            int dummy = 0;

            while (dummy < itemCount)
            {
                if ((itemCount - dummy) >= maxPivotPageItemCount)
                    dummy += maxPivotPageItemCount;
                else
                    dummy += (itemCount - dummy);

                PivotItem pi = new PivotItem();


                pi.Tag = previousCount;

                String header = "";
                if(previousCount == 0)
                    header = (previousCount + 1) + " - " + dummy;
                else
                    header = previousCount + " - " + dummy;

                pi.Header = header;
                Grid grid = new Grid();
                pi.Content = grid;    
                ListBox ListBoxSearchResults = new ListBox();
                ListBoxSearchResults.ItemTemplate = (DataTemplate)Resources["SearchResultListItemTemplate"];
                grid.Children.Add(ListBoxSearchResults);

                previousCount = dummy + 1;

                PivotSearchResults.Items.Add(pi);
            }
        }

        private void PivotSearchResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(e.AddedItems[0] != null)
                ControllerHelper.SearchResultController.Search(10, (int)(e.AddedItems[0] as PivotItem).Tag, null);
            else if(e.RemovedItems[0] != null)
                ControllerHelper.SearchResultController.Search(10, (int)(e.RemovedItems[0] as PivotItem).Tag, null);
        }
    }
}